package com.lanhai.longheng.localized;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 特殊路段处理
 * @author zhengwei
 *
 */ 
@Service
public class LHRoadMap {
	
	@Autowired
	JedisPool jedisPool;
	
	
	static Map<String, LHRoadBean> map = Collections.synchronizedMap(new HashMap<String, LHRoadBean>());
	static Map<Integer, String> ksmap = Collections.synchronizedMap(new HashMap<Integer, String>());
	static Map<Integer, String> zrmap = Collections.synchronizedMap(new HashMap<Integer, String>());
	static Object locker =new Object();
	
	
	LHRoadBean getBean(String key) {
		synchronized (locker) { 
			LHRoadBean bean = map.get(key);
			if (bean == null) {

				 String mainkey="lhroad_"+key; 
				Gson gson = new Gson();
				Jedis jd = jedisPool.getResource();
				String json = jd.get(mainkey);
				jd.close();

				if (json != null) {
					try {
						bean = gson.fromJson(json, LHRoadBean.class);
					} catch (Exception ex) {
					}
				} 
				if (bean == null) {
					bean = new LHRoadBean();
				}
				map.put(key, bean);
			}
			return bean;
		}
	}

	  void saveCache(String key,LHRoadBean bean ) {
		 map.put(key, bean);
		  
		 synchronized (locker) {
			 String mainkey="lhroad_"+key; 
				Gson gson = new Gson();
				String json=gson.toJson(bean); 
				Jedis jd = jedisPool.getResource();
				jd.set(mainkey,json);
				jd.close();
		}
		
	}
	
	
	// 扩散那一段的
	
	public  void ksEnterAgv(boolean toleft,int agvid) {
		String key="KS";
		LHRoadBean bean=getBean(key);
		bean.setToLeft(toleft); 
		bean.addAgv(agvid);
		 
		ksmap.put(agvid, "");
		
		saveCache(key,bean);
	}
	
	public    boolean ksCanLeft(int agvid) {
		String key="KS";
		LHRoadBean bean=getBean(key);
		//原来的往右，但是数量是0，或者列表里有当前agvid
		if(bean.isToLeft()==false
				&& (bean.getList().size()==0 || bean.getList().contains(agvid) )
				) {
			return true; 
		}
		//原来的往左即可
		if(bean.isToLeft() ) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * 允许往右
	 * @return
	 */
	public     boolean ksCanRight(int agvid) {
		String key="KS";
		LHRoadBean bean=getBean(key);
		//原来的往左，但是数量是0的可以
		if(bean.isToLeft() 
				&& (bean.getList().size()==0 || bean.getList().contains(agvid) )
				) {
			return true; 
		}
		//往右，但是只能过一个
		if(bean.isToLeft()==false && (bean.getList().size()==0 || bean.getList().contains(agvid) )
				) {
			return true;
		}
		return false;
	}
	
	public    void ksLeaveAgv(int agvid) {
		String key="KS";
		LHRoadBean bean=getBean(key); 
		bean.removeAgv(agvid);
		saveCache(key,bean);
		
		ksmap.remove(agvid);
	}
	
	
	
	//制绒缓存机后台
	
	public    void zrEnterAgv(boolean toleft,int agvid) {
		String key="ZR";
		LHRoadBean bean=getBean(key);
		bean.setToLeft(toleft); 
		bean.addAgv(agvid);
		saveCache(key,bean);
		
		zrmap.put(agvid , "");
	}
	
	public    boolean zrCanLeft(int agvid) {
		String key="ZR";
		LHRoadBean bean=getBean(key);
		//原来的往右，但是数量是0，
		if(bean.isToLeft()==false&& (bean.getList().size()==0 || bean.getList().contains(agvid) )
				) {
			//System.out.println("原来的往右，但是数量是0，可以走");
			return true; 
		}
		//原来的往左，继续往左 
		if(bean.isToLeft() ) {
			//System.out.println("本来就左，可以走");
			return true;
		} 
		return false;
	}
	
	
	/**
	 * 允许往右
	 * @return
	 */
	public   synchronized boolean zrCanRight(int agvid) {
		String key="ZR";
		LHRoadBean bean=getBean(key);
		//原来的往左，但是数量是0，
		if(bean.isToLeft()&& (bean.getList().size()==0 || bean.getList().contains(agvid) )
				) {
			return true; 
		}
		//原来的往右，继续往右
		if(bean.isToLeft()==false) {
			return true;
		}
		return false;
	}
	
	public  synchronized void zrLeaveAgv(int agvid) {
		String key="ZR";
		LHRoadBean bean=getBean(key);
		bean.removeAgv(agvid);
		saveCache(key,bean);
		
		zrmap.remove(agvid);
	}
	
	//清空
	public  synchronized void zrZero() {
		String key="KS";
		LHRoadBean bean=getBean(key);
		bean.reset();
		saveCache(key,bean);
		
		zrmap.clear();
	}
	
	public  synchronized void ksZero() {
		String key="ZR";
		LHRoadBean bean=getBean(key);
		bean.reset();
		saveCache(key,bean);
		
		ksmap.clear();
	}
	
	public synchronized void clearAgv(int agvid) {
		if(ksmap.containsKey(agvid)) {
			ksLeaveAgv(agvid);
		}
		if(zrmap.containsKey(agvid)) {
			zrLeaveAgv(agvid);
		}
	}
}
